/*
 * Module 02
 *
 */
package ua.gl.basecamp.tabachnyy;

import java.io.*;
import java.util.*;
import ua.gl.basecamp.tabachnyy.testcase.*;
import ua.gl.basecamp.tabachnyy.util.*;

/**
 * @author yaroslav.tabachnyy@gmail.com
 * @version 1.02
 * @date 14.02.2013
 *
 */
public class TesterTrainees {

    private List<Question> questions;
    private ScoreCounter scoreCounter;
    private TesterIO testerIO;
    private QuestionListFactory questionListFactory;

    public TesterTrainees(QuestionListFactory questionListFactory) {
        this(questionListFactory, System.in, System.out);
    }

    public TesterTrainees(QuestionListFactory questionListFactory,
            InputStream in, OutputStream out) {
        this.questionListFactory = questionListFactory;
        testerIO = new TesterIO(in, out);
    }

    public void startTest() {
        this.setQuestions();
        this.shuffleTestCases();
        scoreCounter = new ScoreCounter();
        for (Question question : questions) {
            testerIO.outQuestion(question);
            List<UserAnswer> userAnswers = testerIO.getUserAnswers(question);
            scoreCounter.calculateScore(question, userAnswers);
        }
        testerIO.outResult(scoreCounter.getScore());
    }

    private void setQuestions() {
        questions = questionListFactory.createQuestions();
    }

    private void shuffleTestCases() {
        Collections.shuffle(questions);
        for (Question question : questions) {
            question.shuffleAnswers();
        }
    }
}
